<template>
  <div class="detail-container">
    <div class="header-nav">
      <div class="logo">“沈誉通”信用生活综合系统</div>
      <div class="nav">
        <!-- <router-link to="/home">首页</router-link> -->
        <router-link to="/credit-life">信用生活</router-link>
        <router-link to="/credit-business">信用商业</router-link>
        <router-link to="/behavior">亲社会行为</router-link>
        <router-link to="/news">新闻资讯</router-link>
        <router-link to="/credit-management">信用管理</router-link>
      </div>
    </div>
    <div class="back-bar">
      <el-button icon="el-icon-arrow-left" @click="$router.push('/credit-life')" type="text">返回信用生活</el-button>
    </div>
    <div class="detail-main">
      <h2>舒心就医</h2>
      <div class="hospital-list">
        <div v-for="hospital in hospitals" :key="hospital.id" class="hospital-card">
          <div class="hospital-info">
            <h3>{{ hospital.name }}</h3>
            <p class="address">地址：{{ hospital.address }}</p>
            <p class="level">等级：{{ hospital.level }}</p>
            <p class="desc">简介：{{ hospital.desc }}</p>
          </div>
          <el-button type="primary" :disabled="hospital.appointed" @click="appoint(hospital)">
            {{ hospital.appointed ? '已预约' : '预约挂号' }}
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "HospitalDetail",
  data() {
    return {
      hospitals: [
        {
          id: 1,
          name: '杭州市第一人民医院',
          address: '杭州市上城区解放路261号',
          level: '三甲',
          desc: '综合性大型医院，提供多科室诊疗服务',
          appointed: false
        },
        {
          id: 2,
          name: '浙江大学医学院附属邵逸夫医院',
          address: '杭州市江干区庆春东路3号',
          level: '三甲',
          desc: '以高水平医疗和优质服务著称',
          appointed: false
        },
        {
          id: 3,
          name: '杭州市中医院',
          address: '杭州市上城区邮电路453号',
          level: '三甲',
          desc: '中西医结合，特色专科丰富',
          appointed: false
        }
      ]
    };
  },
  created() {
    this.recordClick();

  },
  methods: {
    async recordClick() {
      try {
        const { data: userData } = await axios.get('/users/user/basic', {
          headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
        });
        if (userData.success && userData.data) {
          this.user = userData.data;
        } else {
          this.$message.error('获取用户信息失败：无有效数据');
        }
      } catch (error) {
        this.$message.error('获取用户信息失败');
        console.error('获取用户信息失败:', error);
      }
      const userId = this.user.id;

      axios.post(`/commodity/click/hospital?userId=${userId}`, {
        headers: { Authorization: `Bearer ${localStorage.getItem('token')}` },
      });
    },
    appoint(hospital) {
      if (hospital.appointed) return;
      this.$set(hospital, 'appointed', true);
      this.$message.success(`已成功预约${hospital.name}！`);
    }
  }
}
</script>

<style scoped>
.detail-container {
  min-height: 100vh;
  background: radial-gradient(ellipse at center, #001e3c 0%, #000 100%);
  color: #fff;
}
.header-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 30px;
  background: #002d52;
}
.logo {
  font-size: 20px;
  color: #4fc3f7;
}
.nav {
  display: flex;
  gap: 20px;
}
.nav a {
  color: #fff;
  text-decoration: none;
  padding: 5px 10px;
  border-radius: 4px;
  transition: all 0.3s ease;
}
.nav a:hover {
  color: #4fc3f7;
}
.back-bar {
  margin: 20px 0 0 0;
  max-width: 800px;
  margin-left: auto;
  margin-right: auto;
}
.detail-main {
  max-width: 900px;
  margin: 40px auto;
  background: #13263a;
  border-radius: 10px;
  padding: 40px 30px;
  box-shadow: 0 5px 15px rgba(79, 195, 247, 0.1);
}
.hospital-list {
  display: flex;
  flex-direction: column;
  gap: 30px;
}
.hospital-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: #0a2744;
  border-radius: 10px;
  padding: 24px 30px;
  border: 1px solid #1e88e5;
}
.hospital-info h3 {
  color: #4fc3f7;
  margin-bottom: 8px;
  font-size: 20px;
}
.hospital-info .address,
.hospital-info .level,
.hospital-info .desc {
  color: #b0bec5;
  font-size: 15px;
  margin-bottom: 4px;
}
</style>
